package guiWindows;

import javax.swing.JPanel;

import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import javax.swing.JButton;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.BoxLayout;

import java.awt.Color;
import java.awt.event.*;

import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;

import flwa_to_fsp.FLWAtoFSP;

import lts.LTSException;
import lts.ltl.AssertDefinition;
import main.LTSA;
import mediator.AddPropertyMediator;
import mediator.ShowPropertyMediator;

import objectsDiagram.Fluent;
import objectsDiagram.Task;

public class GUICreateProperties extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel areasPanel = null;
	private JPanel addPanel;
	private JScrollPane scrollAreas;
	private JList<String> listCommand;
	private DefaultListModel<String> listModel;
	private int numberProperty;
	private JButton addButton;
	private JButton deleteButton;
	private JButton checkPropertyButton;
	private JButton backButton;
	private Vector<Task> vectorTask;
	private LinkedList<Fluent> listFluents;
	private LinkedList<PanelArea> listPanelAreas;
	private ActionListener addPropertyMediator;

	public GUICreateProperties(AddPropertyMediator addPropertyMediator) {
		super();
		this.addPropertyMediator = addPropertyMediator;
		this.vectorTask = addPropertyMediator.getVectorTasks();
		initialize();
	}

	private void initialize() {
		this.setLayout(new BorderLayout());
		this.add(getListCommand(), BorderLayout.WEST);
		this.add(getScrollAreas(), BorderLayout.CENTER);
		this.add(getPanelButton(), BorderLayout.SOUTH);
		this.listPanelAreas = new LinkedList<PanelArea>();
		this.setBorder(BorderFactory.createTitledBorder(null, "Properties", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
	}

	public JList<String> getListCommand(){
		if(listCommand == null){
			listCommand = new JList<String>(getDefaultListModel());
			getDefaultListModel().addElement("\\/");
			getDefaultListModel().addElement("/\\");
			getDefaultListModel().addElement("->");
			getDefaultListModel().addElement("!");
			getDefaultListModel().addElement("[]");
			getDefaultListModel().addElement("<>");
			getDefaultListModel().addElement("W");
			getDefaultListModel().addElement("X");
			listCommand.setBackground(new Color(210,210,210));
			listCommand.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
			listCommand.setDragEnabled(true);
		}
		return listCommand;
	}

	public DefaultListModel<String> getDefaultListModel(){
		if(listModel == null){
			listModel = new DefaultListModel<String>();
		}
		return listModel;
	}

	private JPanel getPanelButton(){
		if(addPanel == null){
			addPanel = new JPanel();
			addPanel.setLayout(new FlowLayout());
			addPanel.add(getAddButton());
			addPanel.add(getDeleteButton());
			addPanel.add(getCheckPropertyButton());
			addPanel.add(getBackButton());
		}
		return addPanel;
	}

	public LinkedList<PanelArea> getListPanelAreas() {
		return listPanelAreas;
	}

	public void setListPanelAreas(LinkedList<PanelArea> listPanelAreas) {
		this.listPanelAreas = listPanelAreas;
	}

	public int getNumberProperty() {
		return numberProperty;
	}

	public void setNumberProperty(int numberProperty) {
		this.numberProperty = numberProperty;
	}

	public JButton getAddButton(){
		if(addButton == null){
			addButton = new JButton("Add Property");
			addButton.addActionListener(this.addPropertyMediator);
			addButton.setEnabled(false);
		}
		return addButton;
	}

	public JButton getDeleteButton(){
		if(deleteButton == null){
			deleteButton = new JButton("Delete Property");
			deleteButton.addActionListener(this.addPropertyMediator);
			deleteButton.setEnabled(false);
		}
		return deleteButton;
	}

	public JButton getCheckPropertyButton(){
		if(checkPropertyButton == null){
			checkPropertyButton = new JButton("Check Property");
			checkPropertyButton.addActionListener(this.addPropertyMediator);
			checkPropertyButton.setEnabled(false);
		}
		return checkPropertyButton;
	}
	
	public JButton getBackButton(){
		if(backButton == null){
			backButton = new JButton("Back Results");
			backButton.addActionListener(this.addPropertyMediator);
			backButton.setVisible(false);
		}
		return backButton;
	}

	public JPanel getAreasPanel() {
		if (areasPanel == null) {
			areasPanel = new JPanel();
			areasPanel.setLayout(new BoxLayout(getAreasPanel(), BoxLayout.PAGE_AXIS));
		}
		return areasPanel;
	}

	private JScrollPane getScrollAreas() {
		if (scrollAreas == null) {
			scrollAreas = new JScrollPane(getAreasPanel());
			scrollAreas.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		}
		return scrollAreas;
	}

	public PanelArea addNewAreaPanel(){
		numberProperty++;
		PanelArea newAreaPanel = new PanelArea(numberProperty,vectorTask);
		newAreaPanel.setActionListener(this.addPropertyMediator);
		listPanelAreas.addLast(newAreaPanel);
		getAreasPanel().add(newAreaPanel);
		return newAreaPanel;
	}

	public void clearProperties(){
		this.numberProperty = 0;
		getAreasPanel().removeAll();
		getListPanelAreas().clear();
		getDeleteButton().setEnabled(false);
		getCheckPropertyButton().setEnabled(false);
	}

	public LinkedList<PanelArea> getPanelAreas(){
		return listPanelAreas;
	}

	public JPanel getPanelAreaAtIndex(int index){
		return listPanelAreas.get(index);
	}
	//-------------------------------------------------------------------------
	//-------------------------------------------------------------------------	

	public void setVectorTask(Vector<Task> newVectorTask){
		this.vectorTask = newVectorTask;
	}

	public void setListFluent(LinkedList<Fluent> newListFluent){
		this.listFluents = newListFluent;
	}
	
	public Vector<Task> getVectorTask(){
		return this.vectorTask;
	}

	public LinkedList<Fluent> getListFluent(){
		return this.listFluents;
	}


}
